package com.chucang.shucang.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/9/14 21:30
 * @description 限流配置 ip限流
 */
@Configuration(proxyBeanMethods = false)
public class RateLimitConfig {

    @Bean
    public KeyResolver remoteAddrResolver() {
        return exchange -> Mono
                .just(Objects.requireNonNull(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()))
                        .getAddress().getHostAddress());
    }
}
